{% extends 'car_system/base.html' %}

{% block title %}编辑用户 - 管理后台{% endblock %}

{% block content %}
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{% url 'home' %}">首页</a></li>
        <li class="breadcrumb-item"><a href="{% url 'admin_dashboard' %}">管理后台</a></li>
        <li class="breadcrumb-item active" aria-current="page">编辑用户: {{ user_obj.username }}</li>
    </ol>
</nav>

<div class="card">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0"><i class="fas fa-user-edit me-2"></i>编辑用户: {{ user_obj.username }}</h5>
    </div>
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <div class="row">
                <!-- 基本信息 -->
                <div class="col-md-6">
                    <h5 class="mb-3">基本信息</h5>
                    
                    <div class="mb-3">
                        <label for="{{ u_form.username.id_for_label }}" class="form-label">用户名</label>
                        {{ u_form.username }}
                        {% if u_form.username.errors %}
                        <div class="text-danger">
                            {% for error in u_form.username.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        <label for="{{ u_form.email.id_for_label }}" class="form-label">电子邮件</label>
                        {{ u_form.email }}
                        {% if u_form.email.errors %}
                        <div class="text-danger">
                            {% for error in u_form.email.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                        {% endif %}
                    </div>
                    
                    <h5 class="mt-4 mb-3">用户权限</h5>
                    
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_active" name="is_active" {% if user_obj.is_active %}checked{% endif %}>
                        <label class="form-check-label" for="is_active">账号激活</label>
                        <div class="form-text">用户是否可以登录系统</div>
                    </div>
                    
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_staff" name="is_staff" {% if user_obj.is_staff %}checked{% endif %}>
                        <label class="form-check-label" for="is_staff">管理员权限</label>
                        <div class="form-text">用户是否可以访问管理后台</div>
                    </div>
                    
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_superuser" name="is_superuser" {% if user_obj.is_superuser %}checked{% endif %}>
                        <label class="form-check-label" for="is_superuser">超级管理员权限</label>
                        <div class="form-text">用户拥有所有权限，不受权限限制</div>
                    </div>
                </div>
                
                <!-- 用户资料信息 -->
                <div class="col-md-6">
                    <h5 class="mb-3">用户资料</h5>
                    
                    <div class="mb-3">
                        <label for="{{ p_form.phone.id_for_label }}" class="form-label">电话号码</label>
                        {{ p_form.phone }}
                        {% if p_form.phone.errors %}
                        <div class="text-danger">
                            {% for error in p_form.phone.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        <label for="{{ p_form.avatar.id_for_label }}" class="form-label">头像</label>
                        {% if user_obj.profile.avatar %}
                        <div class="mb-2">
                            <img src="{{ user_obj.profile.avatar.url }}" alt="{{ user_obj.username }}" class="img-thumbnail" style="max-width: 100px;">
                        </div>
                        {% endif %}
                        {{ p_form.avatar }}
                        {% if p_form.avatar.errors %}
                        <div class="text-danger">
                            {% for error in p_form.avatar.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                        {% endif %}
                    </div>
                    
                    <hr>
                    
                    <h5 class="mb-3">账号信息</h5>
                    <div class="card">
                        <div class="card-body">
                            <div class="mb-3">
                                <strong>用户 ID:</strong> {{ user_obj.id }}
                            </div>
                            <div class="mb-3">
                                <strong>注册日期:</strong> {{ user_obj.date_joined|date:"Y-m-d H:i" }}
                            </div>
                            <div class="mb-3">
                                <strong>上次登录:</strong> {{ user_obj.last_login|date:"Y-m-d H:i" }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <hr>
            
            <div class="d-flex justify-content-between">
                <a href="{% url 'admin_dashboard' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-1"></i> 返回
                </a>
                <div>
                    <a href="{% url 'admin_user_delete' user_obj.id %}" class="btn btn-outline-danger me-2">
                        <i class="fas fa-trash me-1"></i> 删除用户
                    </a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i> 保存修改
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
{% endblock %} 